package com.mt.mtyx.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.mtyx.common.exception.BusinessException;
import com.mt.mtyx.common.result.Result;
import com.mt.mtyx.entity.product.AttrGroup;
import com.mt.mtyx.product.service.AttrGroupService;
import com.mt.mtyx.vo.product.AttrGroupQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author WMT
 * @Date 2023/7/20 10:33
 */
@Api(tags = "平台属性分组")
@RequestMapping("/admin/product/attrGroup")
@RestController
public class AttrGroupController {


    @Autowired
    private AttrGroupService attrGroupService;


    @ApiOperation("分页条件查询平台属性分组")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<AttrGroup>> selectAll(
            @PathVariable Long page,
            @PathVariable Long limit,
            AttrGroupQueryVo queryVo
    ) {
        return Result.success(attrGroupService.selectAll(new Page<AttrGroup>(page, limit), queryVo));
    }


    @ApiOperation("根据id获取平台属性分组详情")
    @GetMapping("/get/{id}")
    public Result<AttrGroup> getDetails(@PathVariable Long id) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(attrGroupService.getDetails(id));
    }


    @ApiOperation("新增平台属性分组")
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody AttrGroup attrGroup) {
        if (StringUtils.isEmpty(attrGroup.getName())) {
            throw new BusinessException("组名不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (attrGroupService.existsByName(attrGroup.getName())) {
            throw new BusinessException("组名已存在！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(attrGroupService.insert(attrGroup));
    }

    @ApiOperation("删除平台属性分组")
    @DeleteMapping("/remove/{id}")
    public Result<Integer> remove(@PathVariable Long id) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(attrGroupService.delete(id));
    }

    @ApiOperation("批量删除平台属性分组")
    @DeleteMapping("/batchRemove")
    public Result<Integer> batchRemove(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(attrGroupService.batchDelete(ids));
    }

    @ApiOperation("修改平台属性分组")
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody AttrGroup attrGroup) {
        if (StringUtils.isEmpty(attrGroup.getId())) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (StringUtils.isEmpty(attrGroup.getName())) {
            throw new BusinessException("组名不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        AttrGroup attrGroupDb = attrGroupService.getById(attrGroup.getId());
        if (!attrGroupDb.getName().equals(attrGroup.getName())) {
            if (attrGroupService.existsByName(attrGroup.getName())) {
                throw new BusinessException("组名已存在！", HttpStatus.BAD_REQUEST.value());
            }
        }
        return Result.success(attrGroupService.update(attrGroup));
    }

    @ApiOperation("获取全部属性分组")
    @GetMapping("findAllList")
    public Result<List<AttrGroup>> findAllList() {
        return Result.success(attrGroupService.findAllList());
    }
}
